package pl.edu.agh.agents.gui.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import pl.edu.agh.agents.gui.GUI;

public class IterationThreadAction extends AbstractAction {
	private static final long serialVersionUID = 7176906203184664531L;

	private IterationThread thread;
	private int startStop;
	private GUI gui;
	public static final int START = 0;
	public static final int STOP = 1;

	public IterationThreadAction(String name, IterationThread thread, GUI gui) {
		super(name);

		this.thread = thread;
		this.startStop = START;
		this.gui = gui;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (startStop == START) {
			gui.startIterationThread();
			if (thread.isAlive()) {
				synchronized (thread) {
					thread.notify();
				}
			}
			else {
				thread.start();
			}
		}
		else {
			gui.stopIterationThread();
		}
		
		startStop = (startStop+1) % 2;
	}
}